package com.cmcc.linlaker.ui.query.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.ui.query.ProjectDetailActivity;
import com.cmcc.linlaker.ui.query.adapter.QueryProjectApproveDetailAdapter;
import com.cmcc.linlaker.ui.query.bean.QueryProjectBean;
import com.cmcc.linlaker.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class ProjectApproveDetailFragment extends Fragment {
    private View fragmentView;

    public static ProjectApproveDetailFragment newInstance() {
        ProjectApproveDetailFragment fragment = new ProjectApproveDetailFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.fragment_query_project_approve, container, false);
        initView();
        return fragmentView;
    }

    private RecyclerView recycler_project_approve_detail;
    private QueryProjectApproveDetailAdapter adapter;
    private List<QueryProjectBean.ProjectApprovalData> itemList = new ArrayList<>();

    private void initView() {
        recycler_project_approve_detail = fragmentView.findViewById(R.id.recycler_project_approve_detail);
        setRecyclerView(recycler_project_approve_detail, itemList);

        if (((ProjectDetailActivity) getActivity()).projectDetailData != null) {
            fragmentView.findViewById(R.id.text_no_data).setVisibility(View.GONE);
            recycler_project_approve_detail.setVisibility(View.VISIBLE);
            QueryProjectBean.QueryProjectData projectDetailData = ((ProjectDetailActivity) getActivity()).projectDetailData;
            itemList.clear();
            if (projectDetailData.projectApprovals != null && projectDetailData.projectApprovals.size() > 0) {
                itemList.addAll(projectDetailData.projectApprovals);
                adapter.notifyDataSetChanged();
            } else {
                fragmentView.findViewById(R.id.text_no_data).setVisibility(View.VISIBLE);
                recycler_project_approve_detail.setVisibility(View.GONE);
            }
        }
    }

    private void setRecyclerView(final RecyclerView recycler_view, final List<QueryProjectBean.ProjectApprovalData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(getActivity()));

        recycler_view.setNestedScrollingEnabled(false);
        adapter = new QueryProjectApproveDetailAdapter(getActivity(), itemList);
        adapter.setOnItemClickListener(new QueryProjectApproveDetailAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });
        recycler_view.setAdapter(adapter);
    }
}
